/*
   Copyright (c) 2012, 2015 Mizar Tools Contributors (mizartools.org)

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
/*  Contributors :
 *	2012-04-09 Marco Riccardi - initial implementation
 *  2015-01-11 Marco Riccardi - added Loci
 *
 */
package org.mizartools.dli;

public class Sethood extends ItemDefinition {

	private Loci loci = null;
	private Type type = null;
	
	private Sethood(){
		super(null);
	}

	public Sethood(Loci loci, Type type) throws DliException {
		super(null);
		if (type == null) {
			throw new DliException();
		}
		this.loci = loci;
		this.type = type;
	}
	
	public static ItemDefinition newInstance(String string) throws DliParseException {
		Sethood sethood = new Sethood();
		int position = string.indexOf("(");
		if (position == -1) {
			throw new DliParseException("["+string+"] is not valid in Sethood");
		}
		if (!string.substring(0, position).equals("sethood")) {
			throw new DliParseException("["+string+"] is not valid in Sethood");
		}
		string = string.substring(position+1, string.length()-1);
		position = string.indexOf("$loci(");
		if (position != -1) {
			int positionComma = Utility.searchSeparationComma(string);
			sethood.loci = Loci.newInstance(string.substring(0, positionComma));
			string = string.substring(positionComma+1, string.length());
		}
		position = string.indexOf("$type(");
		if (position == -1) {
			throw new DliParseException("["+string+"] is not valid in Sethood");
		}
		sethood.type = Type.newInstance(string);
		return sethood;
	}

	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append("sethood(");
		if (loci != null) {
			if (loci.getTypeList().size() != 0) {
				sb.append(loci.toString());
				sb.append(",");
			}
		}
		sb.append(type.toString());
		sb.append(")");
		return sb.toString();
	}

	public Type getType() {
		return type;
	}

}
